home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * %%File: lib.c
- *
- * Chaque nom de fonction definie dans ce fichier est precede
- * des lettres : lib
- *
- * Copyright (c) Bertrand LE QUELLEC 1995-1999
- *
- * http://perso.wanadoo.fr/blq
- * blq@wanadoo.fr
- */
-
-
- #include <stdio.h>
-
- #ifdef IRIX
- #include <string.h>
- #include <stddef.h>
- #include <ctype.h>
- #endif
-
- #ifndef UNIX_SRC
- #include <string.h>
- #include <stdlib.h>
- #include <malloc.h>
- #endif
-
- #define SOURCE_LIB 1
- #include "lib.h"
-
-
-
- static char libTmpTab[MLEN * 4];
- static int oldChar;
-
-
- /*
- * %%Function: libPairImpair
- *
- * Renvoir True si chiffre pair ou False si impair.
- */
- Bool libPairImpair(int n)
- {
- char tabTmp [512];
-
-
- if (!n) return True;
-
- sprintf(tabTmp, "%d", n);
-
- switch(tabTmp[strlen(tabTmp) - 1])
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 9 :
- return False;
-
- case 0:
- case 2:
- case 4:
- case 6:
- case 8 :
- return True;
-
- default :
- return False;
- }
- }
-
- /*
- * %%Function: libBitCount
- *
- * Inverse une chaine.
- */
- int libBitCount(unsigned n)
- {
- int b;
-
- for(b = 0; n != 0; n >>= 1)
- {
- if(n & 01)
- b++;
- }
-
- return b;
- }
-
- /*
- * %%Function: libReverse
- *
- * Inverse une chaine.
- */
- void libReverse(char chaine[])
- {
- int c, i, j;
-
- for (i = 0, j = strlen(chaine) - 1; i < j; i++, j--)
- {
- c = chaine[i];
- chaine[i] = chaine[j];
- chaine[i] = c;
- }
- }
-
- /*
- * %%Function: libQuote
- *
- * Protege le caractere "
- */
- char * libQuote(char * chaine)
- {
- register int i = 0, j = 0;
-
-
- if(!chaine)
- return chaine;
-
- if(strlen(chaine) > MLEN * 3)
- return chaine;
-
- sprintf(libTmpTab, "%s", chaine);
-
- while(libTmpTab[i] != '\0')
- {
- if(libTmpTab[i] == '"')
- {
- for(j = strlen(libTmpTab); j >= i; j--)
- libTmpTab[j+1] = libTmpTab[j];
-
- libTmpTab[i++] = '\\';
- }
-
- i++;
- }
-
- return libTmpTab;
- }
-
- /*
- * %%Function: libDelQuote
- *
- * Supprime le caractere "
- */
- char * libDelQuote(char * chaine)
- {
- register int i = 0, j = 0;
-
-
- if(!chaine)
- return chaine;
-
- if(strlen(chaine) > MLEN * 3)
- return chaine;
-
- sprintf(libTmpTab, "%s", chaine);
-
- while(libTmpTab[i] != '\0')
- {
- if(libTmpTab[i] == '"')
- {
- for(j = i; j < (int) strlen(libTmpTab); j++)
- libTmpTab[j] = libTmpTab[j+1];
- }
-
- i++;
- }
-
- return libTmpTab;
- }
-
- /*
- * %%Function: libChangeChar
- *
- * Supprime tous les espaces (blancs) situes en fin de chaine.
- */
- char * libChangeChar(char * chaine, char car, char change)
- {
- register int i = 0;
- int len = strlen(chaine);
-
-
- if(!chaine)
- return chaine;
-
- if(len > MLEN * 3)
- return chaine;
-
- sprintf(libTmpTab, "%s", chaine);
-
- while(i < len)
- if(libTmpTab[i++] == car)
- libTmpTab[i-1] = change;
-
- return libTmpTab;
- }
-
- /*
- * %%Function: libRTrim
- *
- * Supprime tous les espaces (blancs) situes en fin de chaine.
- */
- char * libRTrim(char * chaine)
- {
- if(!chaine)
- return chaine;
-
- while(chaine[strlen(chaine)-1] == ' ' || chaine[strlen(chaine)-1] == '\t')
- chaine[strlen(chaine)-1] = '\0';
-
- return chaine;
- }
-
- /*
- * %%Function: libLTrim
- *
- * Supprime tous les espaces (blancs) situes en debut de chaine.
- */
- char * libLTrim(char * chaine)
- {
- register int ct = 0;
-
-
- if(!chaine)
- return chaine;
-
- while(chaine[0] == ' ' || chaine[0] == '\t')
- {
- for(ct = 0; ct < (int)strlen(chaine); ct++)
- chaine[ct] = chaine[ct+1];
- }
-
- return chaine;
- }
-
- /*
- * %%Function: libCaseString
- *
- * Conversion d'une chaine de caracteres en majuscule ou en minuscule
- * suivant l'option passee en argument. La chaine retournee est allouee
- * en memoire.
- */
- char * libCaseString(char * chaine, int option)
- {
- char * newChaine = (char *) 0;
- int ct = 0;
-
-
- if (!chaine)
- return chaine;
- if (!(newChaine = (char *)calloc(strlen(chaine), (size_t)sizeof(char))))
- return chaine;
-
- while(ct <= (int)strlen(chaine))
- {
- if(option == UPPERCASE)
- newChaine[ct] = (char)toupper((int)chaine[ct++]);
- else
- newChaine[ct] = (char)tolower((int)chaine[ct++]);
- }
-
- newChaine[ct] = '\0';
-
- return newChaine;
- }
-
-
- /* ------------------------------------------------------------------------------- */
-
-
- /*
- * %%Function: libPrintCharStd
- *
- * Send a character to the std file.
- */
- void libPrintCharStd(int ch, FILE * std)
- {
- if(std)
- putc(ch, std);
- }
-
- /*
- * %%Function: libPrintString
- *
- * Send a string to the std file.
- */
- void libPrintString(char * string, FILE * std)
- {
- if(string && std)
- {
- fflush(std);
- fprintf(std, "%s", string);
- fflush(std);
- }
- }
-
- /*
- * %%Function: libPrintInt
- *
- * Send an integer to the std file.
- */
- void libPrintInt(int integer, FILE * std)
- {
- if(std)
- fprintf(std, "%d", integer);
- }
-
- /*
- * %%Function: libPrintLong
- *
- * Send a Long integer to the std file.
- */
- void libPrintLong(long integer, FILE * std)
- {
- if(std)
- fprintf(std, "%ld", integer);
- }
-
- /*
- * %%Function: libPrintStringInt
- *
- * Send a string and an integer to the std file.
- */
- void libPrintStringInt(char * string, int integer, FILE * std)
- {
- if(std)
- {
- fprintf(std, "%s %d", string, integer);
- fflush(std);
- }
- }
-
- /*
- * %%Function: libPosChar.
- *
- * Renvoie la premiere position d'un caractere dans une chaine,
- * si le caractere n'est pas trouve la fonction renvoie -1.
- *
- * L'option (flag) permet de choisir de lancer la recherche par le
- * debut de la chaine (true) ou par la fin (false).
- */
- int libPosChar(char * chaine, char leChar, bool flag)
- {
- int ct = 0, len = strlen(chaine);
-
- if(flag == false)
- {
- len = len - 1;
- while(len != ct) if(chaine[len--] == leChar) return ++len;
- }
- else
- while(ct < len) if(chaine[ct++] == leChar) return --ct;
-
- return -1;
- }
-
-
- /* ------------------------------------------------------------------------- */
-
-
- /*
- * %%Function: libGetStrToChar.
- *
- * Renvoie une chaine de caracteres jusqu'au caractere donne
- * ou par defaut la fin de ligne (ou fin de fichier).
- */
- char * libGetStrToChar (FILE * fp, int caractere)
- {
- register int ch = 0, ct = 0;
-
-
- if(fp)
- {
- ch = getc(fp);
-
- while (ch != EOF && ch != '\n' && ch != caractere)
- {
- if(ct + 2 < MLEN * 4)
- {
- libTmpTab[ct++] = (char)ch;
- libTmpTab[ct] = '\0';
- }
-
- ch = getc(fp);
- }
- }
-
- oldChar = ch;
-
- return libTmpTab;
- }
-
- /*
- * %%Function: libGetStrToCR.
- *
- * Renvoie une chaine de caracteres jusqu'a la fin de ligne
- * issue d'un fichier.
- */
- char * libGetStrToCR (FILE * fp)
- {
- return libGetStrToChar(fp, '\n');
- }
-
- /*
- * %%Function: libNextLine.
- *
- * On deplace le file pointeur jusqu'au debut de la ligne suivante.
- */
- void libNextLine(FILE * fp)
- {
- register int ch = 0;
-
-
- if(fp)
- {
- ch = getc(fp);
-
- while (ch != EOF && ch != '\n')
- ch = getc(fp);
- }
-
- oldChar = ch;
- }
-
- /*
- * %%Function: libGotoChar.
- *
- * On deplace le file pointeur jusqu'au prochain caractere
- * ou a defaut la fin de ligne (ou fin de fichier).
- */
- void libGotoChar(FILE * fp, int caractere)
- {
- register int ch = 0;
-
-
- if(fp)
- {
- ch = getc(fp);
-
- while (ch != EOF && ch != '\n' && ch != caractere)
- ch = getc(fp);
- }
-
- oldChar = ch;
- }
-
- /*
- * %%Function: libTypeOldChar.
- *
- * Test l'egalite du dernier caractere traite par les fonctions
- * lib en lecture sur un fichier par caractere.
- */
- bool libTypeOldChar(int caractere)
- {
- if(caractere == oldChar)
- return true;
- else
- return false;
- }
-
-